﻿using CJAtech.BBS.DataEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Bigger.Framework;
using Bigger.Web.Mvc;
using Bigger.Web.Helper;

namespace CJAtech.BBS.Process.BBSFilter
{
    public class WXAuthFilter : BiggerActionFilter
    {
        /// <summary>
        /// 权限类型
        /// </summary>
        public BBSPowerType BBSPowerType { get; set; }
        public WXAuthFilter(BBSPowerType bbsPowerType)
        {
            this.BBSPowerType = bbsPowerType;
            this.Order = 0;
        }
        public override void OnActionExecuting(System.Web.Mvc.ActionExecutingContext filterContext)
        {
            if (this.BBSPowerType == BBSPowerType.None)
            {
                return;
            }

            var objCommunityConfig = filterContext.HttpContext.GetItems("CommunityConfig") as CommunityConfig;

            //todo：验证接口还是Cookie；匿名访问

            string wxToken;
            if (SystemWebAsset.IsCrossDomain())//todo:Url有带取Url
            {
                wxToken = filterContext.HttpContext.Request["Token"];
            }
            else
            {
                wxToken = CookieHelper.GetCookieValue(objCommunityConfig.ConfigCode + "WXToken");
            }
            UserInfo objUserInfo = null;
            if (wxToken.IsNoNull())
            {
                if (objCommunityConfig.ValidType == 1)
                {
                    objUserInfo = wxToken.JsonDeserialize<UserInfo>();
                }
                else
                {
                    InvokeResult objInvokeResult = objCommunityConfig.InterfaceUrl.Replace("{openId}", wxToken, System.Text.RegularExpressions.RegexOptions.IgnoreCase).CreateHttpGet<InvokeResult>();
                    if (objInvokeResult.ResultCode == "Success")
                    {
                        objUserInfo = objInvokeResult.Data as UserInfo;
                    }
                }
            }
            if (objUserInfo == null)
            {
                if (objCommunityConfig.AllowGuest != 1)
                {
                    if (SystemWebAsset.IsAjaxRequest() || SystemWebAsset.IsCrossDomain())
                    {
                        filterContext.Result = new JsonResult
                        {
                            Data = new InvokeResult { ResultCode = "NotLogin", ResultMessage = "对不起，请先登录" },
                            JsonRequestBehavior = JsonRequestBehavior.AllowGet
                        };
                        return;
                    }
                }
                else
                {
                    objUserInfo = new UserInfo
                    {
                        UserId = SystemWebAsset.GetClientIP(),
                        UserName = "网友",
                        Avatar = ""
                    };
                }
            }
            filterContext.HttpContext.SetItems("UserInfo", objUserInfo);
        }
    }
}
